<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>数据库管理</title>
<link href="css/default.css" rel="stylesheet" type="text/css" />
<script src="js/page.js"></script>
<jsp:useBean id="database" class="DatabaseConnection.DatabaseConnection" />
<%@include file="connect_database.jsp" %>
<%
request.setCharacterEncoding("utf-8");
String database_name = request.getParameter("database");
String select_types = "<option value='0' selected='selected'>　　　　</option><option value='1'>tinyint</option><option value='2'>smallint</option><option value='3'>mediumint</option><option value='4'>int</option><option value='5'>integer</option><option value='6'>bigint</option><option value='7'>real</option><option value='8'>double</option><option value='9'>float</option><option value='10'>decimal</option><option value='11'>numeric</option><option value='12'>date</option><option value='13'>time</option><option value='14'>timestamp</option><option value='15'>datetime</option><option value='16'>char</option><option value='17'>varchar</option><option value='18'>tinyblob</option><option value='19'>blob</option><option value='20'>mediumblob</option><option value='21'>longblob</option><option value='22'>tinytext</option><option value='23'>text</option><option value='24'>mediumtext</option><option value='25'>longtext</option><option value='26'>enum</option><option value='27'>set</option>";
%>
<script type="text/javascript">
<!--
var select_fields = new Array();
select_fields[0] = ["　　　　"];
<%
String sql_tables = "show tables;";
ResultSet resultset_tables = database.executeQuery(sql_tables, database_name);
String select_tables = "<option value='0' selected='selected'>　　　　</option>";
int index_table = 0;
String name_table = "";
while (resultset_tables != null && resultset_tables.next() == true)
{
	index_table++;
	name_table = resultset_tables.getString("Tables_in_" + database_name);
	select_tables = select_tables + "<option value='" + index_table + "'>" + name_table + "</option>";
%>
select_fields[<%= index_table %>] = new Array();
select_fields[<%= index_table %>][0] = "　　　　";
<%
	String sql_fields = "describe " + name_table + ";";
	ResultSet resultset_fields = database.executeQuery(sql_fields, database_name);
	int index_field = 0;
	String name_field = "";
	while (resultset_fields != null && resultset_fields.next() == true)
	{
		index_field++;
		name_field = resultset_fields.getString("Field");
%>
select_fields[<%= index_table %>][<%= index_field %>] = "<%= name_field %>";
<%
	}
	if (resultset_fields != null)
		resultset_fields.close();
}
if (resultset_tables != null)
	resultset_tables.close();
%>
var index_order = 0;

function chooseHandler(index)
{
	if (document.getElementById("choose_" + index).checked == true)
		document.getElementById("tr_" + index).style.backgroundColor = "#F4F4F4";
	else
		document.getElementById("tr_" + index).style.backgroundColor = "#FFFFFF";
	var choose_all = true;
	for (var i = 0; i < document.getElementsByName("choose").length; i++)
	{
		if (document.getElementsByName("choose").item(i).checked == false)
		{
			choose_all = false;
			break;
		}
	}
	if (choose_all == true)
		document.getElementById("choose_all").checked = true;
	else
		document.getElementById("choose_all").checked = false;
}

function foreignTableChangeHandler(index)
{
	for (var i = document.getElementById("foreignfield_" + index).length - 1; i >= 0; i--)
		document.getElementById("foreignfield_" + index).remove(i);
	var foreign_table_index = document.getElementById("foreigntable_" + index).selectedIndex;
	var foreign_field = null;
	for (var j = 0; j < select_fields[foreign_table_index].length; j++)
	{
		foreign_field = document.createElement("option");
		foreign_field.value = j;
		foreign_field.text = select_fields[foreign_table_index][j];
		document.getElementById("foreignfield_" + index).add(foreign_field);
	}
	document.getElementById("foreignfield_" + index).selectedIndex = 0;
	if (foreign_table_index > 0)
		document.getElementById("foreignfield_" + index).focus();
}

function indexClickHandler(index)
{
	if (document.getElementById("index_" + index).checked == true)
	{
		index_order++;
		document.getElementById("indexorder_" + index).value = index_order;
	}
	else
	{
		var current_index_order = parseInt(document.getElementById("indexorder_" + index).value);
		for (var i = 0; i < document.getElementsByName("indexorder").length; i++)
		{
			if (parseInt(document.getElementsByName("indexorder").item(i).value) > current_index_order)
				document.getElementsByName("indexorder").item(i).value = parseInt(document.getElementsByName("indexorder").item(i).value) - 1;
			else if (parseInt(document.getElementsByName("indexorder").item(i).value) == current_index_order)
				document.getElementsByName("indexorder").item(i).value = "";
		}
		index_order--;
	}
}

function chooseAllHandler()
{
	var choose_all = document.getElementById("choose_all").checked;
	var choose = document.getElementsByName("choose");
	for (var i = 0; i < choose.length; i++)
	{
		choose.item(i).checked = choose_all;
		if (choose_all == true)
			document.getElementById("tr_" + choose.item(i).id.substring("choose_".length)).style.backgroundColor = "#F4F4F4";
		else
			document.getElementById("tr_" + choose.item(i).id.substring("choose_".length)).style.backgroundColor = "#FFFFFF";
	}
}

function insertClickHandler()
{
	var index = parseInt(document.getElementsByName("name").item(document.getElementsByName("name").length - 1).id.substring("name_".length)) + 1;
	var new_row = document.getElementById("table_field").insertRow(document.getElementById("table_field").rows.length - 1);
	new_row.align = "center";
	new_row.bgColor = "#FFFFFF";
	new_row.id = "tr_" + index;
	var new_cell_0 = new_row.insertCell(0);
	new_cell_0.height = 24;
	new_cell_0.innerHTML = "<input name='choose' type='checkbox' id='choose_" + index + "' onclick='chooseHandler(" + index + ");' />";
	var new_cell_1 = new_row.insertCell(1);
	new_cell_1.height = 24;
	new_cell_1.innerHTML = "<input name='name' type='text' id='name_" + index + "' size='5' />";
	var new_cell_2 = new_row.insertCell(2);
	new_cell_2.height = 24;
	new_cell_2.innerHTML = "<select name='type' class='select_menu' id='type_" + index + "'><%= select_types %></select>";
	var new_cell_3 = new_row.insertCell(3);
	new_cell_3.height = 24;
	new_cell_3.innerHTML = "<input name='size' type='text' id='size_" + index + "' size='1' />";
	var new_cell_4 = new_row.insertCell(4);
	new_cell_4.height = 24;
	new_cell_4.innerHTML = "<input name='precision' type='text' id='precision_" + index + "' size='1' />";
	var new_cell_5 = new_row.insertCell(5);
	new_cell_5.height = 24;
	new_cell_5.innerHTML = "<input name='unsigned' type='checkbox' id='unsigned_" + index + "' />";
	var new_cell_6 = new_row.insertCell(6);
	new_cell_6.height = 24;
	new_cell_6.innerHTML = "<input name='zerofill' type='checkbox' id='zerofill_" + index + "' />";
	var new_cell_7 = new_row.insertCell(7);
	new_cell_7.height = 24;
	new_cell_7.innerHTML = "<input name='binary' type='checkbox' id='binary_" + index + "' />";
	var new_cell_8 = new_row.insertCell(8);
	new_cell_8.height = 24;
	new_cell_8.innerHTML = "<input name='enumeration' type='text' id='enumeration_" + index + "' size='14' />";
	var new_cell_9 = new_row.insertCell(9);
	new_cell_9.height = 24;
	new_cell_9.innerHTML = "<input name='notnull' type='checkbox' id='notnull_" + index + "' />";
	var new_cell_10 = new_row.insertCell(10)
	new_cell_10.height = 24;
	new_cell_10.innerHTML = "<input name='default' type='text' id='default_" + index + "' size='5' />";
	var new_cell_11 = new_row.insertCell(11);
	new_cell_11.height = 24;
	new_cell_11.innerHTML = "<input name='autoincremental' type='checkbox' id='autoincremental_" + index + "' />";
	var new_cell_12 = new_row.insertCell(12);
	new_cell_12.height = 24;
	new_cell_12.innerHTML = "<input name='unique' type='checkbox' id='unique_" + index + "' />";
	var new_cell_13 = new_row.insertCell(13);
	new_cell_13.height = 24;
	new_cell_13.innerHTML = "<input name='primarykey' type='checkbox' id='primarykey_" + index + "' />";
	var new_cell_14 = new_row.insertCell(14);
	new_cell_14.height = 24;
	new_cell_14.innerHTML = "<table width='100%' border='0' cellspacing='0' cellpadding='0'><tr align='center'><td><select name='foreigntable' class='select_menu' id='foreigntable_" + index + "' onchange='foreignTableChangeHandler(" + index + ");'><%= select_tables %></select></td><td><select name='foreignfield' class='select_menu' id='foreignfield_" + index + "'><option value='0'>　　　　</option></select></td></tr></table>";
	var new_cell_15 = new_row.insertCell(15);
	new_cell_15.height = 24;
	new_cell_15.innerHTML = "<input name='index' type='checkbox' id='index_" + index + "' onclick='indexClickHandler(" + index + ");' />";
	var new_cell_17 = new_row.insertCell(16);
	new_cell_17.height = 24;
	new_cell_17.innerHTML = "<input name='indexorder' type='text' style='text-align:center' id='indexorder_" + index + "' size='2' readonly='readonly' />";
	document.getElementById("choose_all").checked = false;
	document.getElementById("name_" + index).focus();
}

function deleteClickHandler()
{
	if (document.getElementById("choose_all").checked == true)
	{
		window.alert("无法删除全部的字段，一个数据表至少要包含一个字段！");
		return;
	}
	var deleted = false;
	for (var i = 0; i < document.getElementsByName("choose").length; i++)
	{
		if (document.getElementsByName("choose").item(i).checked == true)
		{
			if (document.getElementsByName("index").item(i).checked == true)
			{
				document.getElementsByName("index").item(i).checked = false;
				indexClickHandler(document.getElementsByName("index").item(i).id.substring("index_".length));
			}
			document.getElementById("table_field").deleteRow(i + 1);
			deleted = true;
			i--;
		}
	}
	if (deleted == false)
		window.alert("您没有选择要删除的字段！");
}

function okClickHandler()
{
	var empty_names = "";
	for (var i = 0; i < document.getElementsByName("name").length; i++)
	{
		if (document.getElementsByName("name").item(i).value.replace(/(^\s*)|(\s*$)/g, "") == "")
			empty_names = empty_names + (i + 1) + ", ";
	}
	if (empty_names != "")
	{
		window.alert("第 " + empty_names.substr(0, empty_names.length - 2) + " 个字段的名称为空，一个数据表的所有字段都要含有名称！");
		document.getElementsByName("name").item(empty_names.substr(0, empty_names.indexOf(",")) - 1).focus();
		return;
	}
	var empty_types = "";
	for (var j = 0; j < document.getElementsByName("type").length; j++)
	{
		if (document.getElementsByName("type").item(j).selectedIndex == 0)
			empty_types = empty_types + (j + 1) + ", ";
	}
	if (empty_types != "")
	{
		window.alert("第 " + empty_types.substr(0, empty_types.length - 2) + " 个字段的类型为空，一个数据表的所有字段都要含有类型！");
		document.getElementsByName("type").item(empty_types.substr(0, empty_types.indexOf(",")) -1).focus();
		return;
	}
	var wrong_sizes = "";
	for (var k = 0; k < document.getElementsByName("size").length; k++)
	{
		if (isNaN(document.getElementsByName("size").item(k).value) == true)
			wrong_sizes = wrong_sizes + (k + 1) + ", ";
	}
	if (wrong_sizes != "")
	{
		window.alert("第 " + wrong_sizes.substr(0, wrong_sizes.length - 2) + " 个字段的长度输入错误！");
		document.getElementsByName("size").item(wrong_sizes.substr(0, wrong_sizes.indexOf(",")) - 1).select();
		return;
	}
	var wrong_precision = "";
	for (var l = 0; l < document.getElementsByName("precision").length; l++)
	{
		if (isNaN(document.getElementsByName("precision").item(l).value) == true)
			wrong_precision = wrong_precision + (l + 1) + ", ";
	}
	if (wrong_precision != "")
	{
		window.alert("第 " + wrong_precision.substr(0, wrong_precision.length - 2) + " 个字段的精度输入错误！");
		document.getElementsByName("precision").item(wrong_precision.substr(0, wrong_precision.indexOf(",")) - 1).select();
		return;
	}
	var empty_foreign_fields = "";
	for (var m = 0; m < document.getElementsByName("foreignfield").length; m++)
	{
		if (document.getElementsByName("foreigntable").item(m).selectedIndex != 0 && document.getElementsByName("foreignfield").item(m).selectedIndex == 0)
			empty_foreign_fields = empty_foreign_fields + (m + 1) + ", ";
	}
	if (empty_foreign_fields != "")
	{
		window.alert("第 " + empty_foreign_fields.substr(0, empty_foreign_fields.length - 2) + " 个字段的连接字段未选择！");
		document.getElementsByName("foreignfield").item(empty_foreign_fields.substr(0, empty_foreign_fields.indexOf(",")) - 1).focus();
		return;
	}
	var name_data = "";
	var type_data = "";
	var size_data = "";
	var precision_data = "";
	var unsigned_data = "";
	var zerofill_data = "";
	var binary_data = "";
	var enumeration_data = "";
	var notnull_data = "";
	var default_data = "";
	var autoincremental_data = "";
	var unique_data = "";
	var primarykey_data = "";
	var foreigntable_data = "";
	var foreignfield_data = "";
	var index_data = "";
	var indexorder_data = "";
	for (var n = 0; n < document.getElementsByName("name").length; n++)
	{
		name_data = name_data + document.getElementsByName("name").item(n).value + " ,";
		type_data = type_data + document.getElementsByName("type").item(n).options.item(document.getElementsByName("type").item(n).selectedIndex).text + " ,";
		size_data = size_data + document.getElementsByName("size").item(n).value + " ,";
		precision_data = precision_data + document.getElementsByName("precision").item(n).value + " ,";
		unsigned_data = unsigned_data + document.getElementsByName("unsigned").item(n).checked + " ,";
		zerofill_data = zerofill_data + document.getElementsByName("zerofill").item(n).checked + " ,";
		binary_data = binary_data + document.getElementsByName("binary").item(n).checked + " ,";
		enumeration_data = enumeration_data + document.getElementsByName("enumeration").item(n).value + " ;";
		notnull_data = notnull_data + document.getElementsByName("notnull").item(n).checked + " ,";
		default_data = default_data + document.getElementsByName("default").item(n).value + " ,";
		autoincremental_data = autoincremental_data + document.getElementsByName("autoincremental").item(n).checked + " ,";
		unique_data = unique_data + document.getElementsByName("unique").item(n).checked + " ,";
		primarykey_data = primarykey_data + document.getElementsByName("primarykey").item(n).checked + " ,";
		foreigntable_data = foreigntable_data + document.getElementsByName("foreigntable").item(n).options.item(document.getElementsByName("foreigntable").item(n).selectedIndex).text + " ,";
		foreignfield_data = foreignfield_data + document.getElementsByName("foreignfield").item(n).options.item(document.getElementsByName("foreignfield").item(n).selectedIndex).text + " ,";
		index_data = index_data + document.getElementsByName("index").item(n).checked + " ,";
		indexorder_data = indexorder_data + document.getElementsByName("indexorder").item(n).value + " ,";
	}
	document.getElementById("name_data").value = name_data;
	document.getElementById("type_data").value = type_data;
	document.getElementById("size_data").value = size_data;
	document.getElementById("precision_data").value = precision_data;
	document.getElementById("unsigned_data").value = unsigned_data;
	document.getElementById("zerofill_data").value = zerofill_data;
	document.getElementById("binary_data").value = binary_data;
	document.getElementById("enumeration_data").value = enumeration_data;
	document.getElementById("notnull_data").value = notnull_data;
	document.getElementById("default_data").value = default_data;
	document.getElementById("autoincremental_data").value = autoincremental_data;
	document.getElementById("unique_data").value = unique_data;
	document.getElementById("primarykey_data").value = primarykey_data;
	document.getElementById("foreigntable_data").value = foreigntable_data;
	document.getElementById("foreignfield_data").value = foreignfield_data;
	document.getElementById("index_data").value = index_data;
	document.getElementById("indexorder_data").value = indexorder_data;
	document.getElementById("form_insert").submit();
}
//-->
</script>
</head>

<body onload="initialize();document.getElementById('name_1').focus();">
<%
String table_name = request.getParameter("table");
String sql_check_table = "show tables like '" + table_name + "';";
ResultSet resultset_check_table = database.executeQuery(sql_check_table, database_name);
if (resultset_check_table != null && resultset_check_table.next() == false)
{
%>
<div style="padding-top:20px" />
<div align="center"><font size="+1"><strong>设计数据表 <%= table_name %> 的字段</strong></font></div>
<br />
<table border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#B1C8D7" id="table_field">
  <tr align="center" bgcolor="#F4F4FF">
    <td height="24"><strong>选择</strong></td>
    <td height="24"><strong>名称</strong></td>
    <td height="24"><strong>类型</strong></td>
    <td height="24"><strong>长度</strong></td>
    <td height="24"><strong>精度</strong></td>
    <td height="24"><strong>无符号</strong></td>
    <td height="24"><strong>自动填零</strong></td>
    <td height="24"><strong>二进制</strong></td>
    <td height="24"><strong>枚举或字符串允许值</strong></td>
    <td height="24"><strong>非空</strong></td>
    <td height="24"><strong>默认值</strong></td>
    <td height="24"><strong>自动增长</strong></td>
    <td height="24"><strong>唯一值</strong></td>
    <td height="24"><strong>主键</strong></td>
    <td height="24"><strong>连接列</strong></td>
    <td height="24"><strong>索引列</strong></td>
    <td height="24"><strong>索引顺序</strong></td>
  </tr>
  <tr align="center" bgcolor="#FFFFFF" id="tr_1">
    <td height="24"><input name="choose" type="checkbox" id="choose_1" onclick="chooseHandler(1);" /></td>
    <td height="24"><input name="name" type="text" id="name_1" size="5" /></td>
    <td height="24"><select name="type" class="select_menu" id="type_1"><%= select_types %></select></td>
    <td height="24"><input name="size" type="text" id="size_1" size="1" /></td>
    <td height="24"><input name="precision" type="text" id="precision_1" size="1" /></td>
    <td height="24"><input name="unsigned" type="checkbox" id="unsigned_1" /></td>
    <td height="24"><input name="zerofill" type="checkbox" id="zerofill_1" /></td>
    <td height="24"><input name="binary" type="checkbox" id="binary_1" /></td>
    <td height="24"><input name="enumeration" type="text" id="enumeration_1" size="14" /></td>
    <td height="24"><input name="notnull" type="checkbox" id="notnull_1" /></td>
    <td height="24"><input name="default" type="text" id="default_1" size="5" /></td>
    <td height="24"><input name="autoincremental" type="checkbox" id="autoincremental_1" /></td>
    <td height="24"><input name="unique" type="checkbox" id="unique_1" /></td>
    <td height="24"><input name="primarykey" type="checkbox" id="primarykey_1" /></td>
    <td height="24">
      <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr align="center">
          <td><select name="foreigntable" class="select_menu" id="foreigntable_1" onchange="foreignTableChangeHandler(1);"><%= select_tables %></select></td>
          <td><select name="foreignfield" class="select_menu" id="foreignfield_1"><option value="0">　　　　</option></select></td>
        </tr>
      </table>
    </td>
    <td height="24"><input name="index" type="checkbox" id="index_1" onclick="indexClickHandler(1);" /></td>
    <td height="24"><input name="indexorder" type="text" style="text-align:center" id="indexorder_1" size="2" readonly="readonly" /></td>
  </tr>
  <tr align="center" bgcolor="#F4F4FF">
    <td height="24">
      <input name="choose_all" type="checkbox" id="choose_all" onclick="chooseAllHandler();" />
      <label for="choose_all"><strong>全选</strong></label>
    </td>
    <td height="24" colspan="16">
      <input name="insert" type="button" class="input_button" id="insert" value="添加字段" onclick="insertClickHandler();" />
      　
      <input name="delete" type="button" class="input_button" id="delete" value="删除字段" onclick="deleteClickHandler();" />
      　
      <input name="ok" type="button" class="input_button" id="ok" value="确定" onclick="okClickHandler();" />
      　
      <input name="back" type="button" class="input_button" id="back" value="返回" onclick="window.location.href='tables.jsp?database=<%= database_name %>';" />
    </td>
  </tr>
</table>
<form id="form_insert" name="form_insert" method="post" action="tables_insert_second_do.jsp?database=<%= database_name %>&table=<%= table_name %>">
  <input type="hidden" name="name_data" id="name_data" />
  <input type="hidden" name="type_data" id="type_data" />
  <input type="hidden" name="size_data" id="size_data" />
  <input type="hidden" name="precision_data" id="precision_data" />
  <input type="hidden" name="unsigned_data" id="unsigned_data" />
  <input type="hidden" name="zerofill_data" id="zerofill_data" />
  <input type="hidden" name="binary_data" id="binary_data" />
  <input type="hidden" name="enumeration_data" id="enumeration_data" />
  <input type="hidden" name="notnull_data" id="notnull_data" />
  <input type="hidden" name="default_data" id="default_data" />
  <input type="hidden" name="autoincremental_data" id="autoincremental_data" />
  <input type="hidden" name="unique_data" id="unique_data" />
  <input type="hidden" name="primarykey_data" id="primarykey_data" />
  <input type="hidden" name="foreigntable_data" id="foreigntable_data" />
  <input type="hidden" name="foreignfield_data" id="foreignfield_data" />
  <input type="hidden" name="index_data" id="index_data" />
  <input type="hidden" name="indexorder_data" id="indexorder_data" />
</form>
<%
}
else if (resultset_check_table == null)
{
%>
<script type="text/javascript">
<!--
	window.alert("连接数据库 <%= database_name %> 失败！");
	window.location.href = "databases.jsp";
//-->
</script>
<%
}
else
{
%>
<script type="text/javascript">
<!--
	window.alert("数据表 <%= table_name %> 已存在！");
	window.history.go(-2);
//-->
</script>
<%
}
if (resultset_check_table != null)
	resultset_check_table.close();
database.closeConnection();
%>
</body>
</html>